﻿<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 数据表格 -->
            <table id="adminTable" lay-filter="adminTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="adminTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs"
       data-dropdown="#adminTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>
<!--    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>-->
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide"
         id="adminTbDelDrop{{d.LAY_INDEX}}"
         style="max-width: 200px;white-space: normal;min-width: auto;margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除{{d.nickname}}吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal" lay-event="del">确定</a>
        </div>
    </div>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="adminTbState">
    <input type="checkbox" lay-filter="adminTbStateCk" value="{{d.uid}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.status==1?'checked':''}}/>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="adminEditDialog">
    <form id="adminEditForm" lay-filter="adminEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">账号:</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入账号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">用户名:</label>
            <div class="layui-input-block">
                <input name="nickname" placeholder="请输入用户名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">备注:</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色:</label>
            <div class="layui-input-block">
                <select name="rid" lay-verType="tips" lay-verify="required" required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">登录密码:</label>
            <div class="layui-input-block">
                <input name="password" type="password" placeholder="请输入登录密码" class="layui-input"
                       lay-verType="tips" lay-verify="required|password" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">确认密码:</label>
            <div class="layui-input-block">
                <input name="check_password" type="password" placeholder="请再输入登录密码" class="layui-input"
                       lay-verType="tips" lay-verify="required|password" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="adminEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'tableX', 'util', 'admin', 'formX', 'dropdown', 'setter','notice'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var util = layui.util;
        var admin = layui.admin;
        var formX = layui.formX;
        var setter = layui.setter;
        var notice = layui.notice;

        var roleList = [];  // 所有角色
        form.render('select', 'adminTbSearchForm');

        /* 渲染表格 */
        var insTb = tableX.render({
            elem: '#adminTable',
            url: setter.baseServer + '/master/master_list',
            page: true,
            defaultToolbar: [],
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加用户</button>',
                '</p>'].join(''),
            height: 'full-185',
            cellMinWidth: 100,
            cols: [[
                // {field: 'id', title: 'id',width: 60},
                {field: 'username', title: '账号'},
                {field: 'nickname', title: '用户名'},
                {field: 'role_name', title: '角色名'},
                {
                    title: '头像', templet: function (d) {
                        var url = d.avatar || 'assets/images/head.png';
                        return '<img data-index="' + (d.LAY_INDEX - 1) + '" src="' + url + '" class="layui-circle" style="height: inherit;"/>';
                    }, align: 'center', width: 90, unresize: true
                },
                {field: 'reg_time', title: '创建时间', width: 160},
                {field: 'login_time', title: '登陆时间',width: 160},
                {field: 'remark', title: '备注'},
                {field: 'state', title: '状态', templet: '#adminTbState', width: 100},
                {title: '操作', toolbar: '#adminTbBar', align: 'center', minWidth: 140, fixed: 'right'}
            ]],
            size:'lg',
            done:function () {
                /* 获取所有角色 */
                admin.req('/role/role_list',{field:'id,name'}, function (res) {
                    if (200 === res.code) {
                        $.each(res.data, function (index, item) {
                            roleList.push({role_name: item.name, id: item.id});
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            }
        });

        /* 表格搜索 */
        form.on('submit(adminTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(adminTable)', function (obj) {
            if ('edit' === obj.event) { // 修改
                showEditModel(obj.data);
            } else if ('del' === obj.event) { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(adminTable)', function (obj) {
            if ('add' === obj.event) { // 添加
                showEditModel();
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '用户',
                content: $('#adminEditDialog').html(),
                success: function (layero, dIndex) {
                    if (mData) {
                        $(layero).find('[name="password"]').removeAttr('lay-verify');
                        $(layero).find('[name="password"]').parent().prev().attr('class','layui-form-label');
                        $(layero).find('[name="check_password"]').removeAttr('lay-verify');
                        $(layero).find('[name="check_password"]').parent().prev().attr('class','layui-form-label');
                    }
                    // 回显表单数据
                    form.val('adminEditForm', mData);
                    // 表单提交事件
                    form.on('submit(adminEditSubmit)', function (data) {
                        if(data.field.password==data.field.check_password){
                            data.field.password=data.field.password;
                        }else{
                            layer.msg('两次输入的密码不一致', {icon: 2});return;
                        }
                        if(mData){
                            data.field.uid=mData.uid;
                        }
                        admin.req((mData ? '/master/master_edit' : '/master/master_add'), JSON.stringify(data.field), function (res) {
                            if (200 === res.code) {
                                layer.close(dIndex);
                                notice.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                notice.msg(res.msg, {icon: 2});
                            }
                        }, 'post');
                        return false;
                    });
                    // 渲染角色下拉
                    formX.renderSelect({
                        elem: '[lay-filter="adminEditForm"] select[name="rid"]',
                        data: roleList,
                        name: 'role_name',
                        value: 'id',
                        hint: '请选择角色',
                        initValue: mData ? mData.rid : undefined
                    });
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 修改用户状态 */
        form.on('switch(adminTbStateCk)', function (obj) {
            notice.msg('加载中...', {icon: 4});
            admin.req('/master/master_status', {uid:obj.elem.value, status: obj.elem.checked ? 1 : 0}, function (res) {
                notice.destroy();
                if (200 === res.code) {
                    notice.msg(res.msg, {icon: 1});
                } else {
                    notice.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox', $('#adminTable').next().attr('lay-filter'));
                }
            }, 'post');
        });
    });
</script>
